package com.cmfi.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cmfi.dao.ReadMeDao;
import com.cmfi.domain.ReadMe;
import com.cmfi.service.inter.ReadMeService;

@Service
public class ReadMeServiceImpl implements ReadMeService {
    @Autowired
    private ReadMeDao readMeDao;
    
    Logger log = LoggerFactory.getLogger(getClass());

    public boolean save(ReadMe readMe) {
        return readMeDao.save(readMe) > 0;
    }
    
    public boolean update(ReadMe readMe, Long id) {
    	readMe.setId(id);
        return readMeDao.update(readMe) > 0;
    }
    
    public boolean delete(Long id) {
        return readMeDao.delete(id) > 0;
    }
    
    public ReadMe getById(Long id) {
        return readMeDao.getById(id);
    }
    
    @Override
	public List<ReadMe> findByPage(Long pageNum, Long pageSize) throws Exception {
		log.info("[ReadMeServiceImpl][findByPage] pageNum:{},pageSize:{}", pageNum, pageSize);
		Long allNum = readMeDao.count();
		long start = (pageNum - 1) * pageSize;
		if (start > allNum) {
			throw new Exception("请求的页已经不存在");
		}
		List<ReadMe> readMeList = readMeDao.findByPage(start, pageSize);
		return readMeList;
	}
    
    @Override
	public List<ReadMe> findAllReadMes() throws Exception {
		log.info("[ReadMeServiceImpl][findByPage] getting error.");
		List<ReadMe> readMeList = readMeDao.findAllReadMes();
		return readMeList;
	}
    
    @Override
   	public ReadMe partialUpdate(ReadMe readMe,Long id) throws Exception {
   		log.debug("Request to partially update ForkliftPlan : {}", readMe);
   		ReadMe existingReadMe = readMeDao.findById(id);
   		if (existingReadMe == null) {
   			throw new Exception("待更新的计划不存在");
   		}
   		if (readMe.getRoleId() != null) {
   			existingReadMe.setRoleId(readMe.getRoleId());
   		}
   		if (readMe.getPath() != null) {
   			existingReadMe.setPath(readMe.getPath());
   		}
   		readMeDao.update(existingReadMe);
   		return existingReadMe;
   	}
    
    @Override
   	public long findRoleId(Long id) throws Exception {
   		long roleId = readMeDao.findRoleId(id);
   		return roleId;
   	}
    
    @Override
   	public List<ReadMe> findByUser(Long roleId) throws Exception {
   		List<ReadMe> limitedreadMeList = readMeDao.findByUser(roleId);
   		return limitedreadMeList;
   	}

}
